/*
 * globals.c
 *
 * Created: 14.09.2011 19:04:53
 *  Author: halfmann
 */ 

#include "globals.h"
#include "uart.h"
#include "lcd.h"
#include "font.h"


void activateSDCardTristate()
{
	MMC_Tristate_PORT |= (1<<MMC_Tristate_PIN);
}


void uartDataParser()
{
	if (mode == PCCONT || mode == PCCONFIG)
		return;
	
	unsigned int uartChar = uart_getc();
	
	//if (uartChar == 0x03 && escape == 1)
	if (uartChar == 'r' && escape == 1)
	{
		breakAnimation = 1;
		mode = PCCONFIG;
		//pcContinousModePending = 1;
		escape = 0;
	}
	if (uartChar == 0x02 && escape == 1)
	{
		breakAnimation = 1;
		mode = PCCONT;
		//pcContinousModePending = 1;
		escape = 0;
	}
	//else if (uartChar == 0xff && escape == 0) //Escape Byte!
	else if (uartChar == 'e' && escape == 0)
	{
		escape = 1;
	}		
	else
		escape = 0;			
											
}


void pcContinousMode()
{
	//pcContinousModeActive = 1;
	//pcContinousModePending = 0;
	
	uint8_t z = 0;
	uint8_t y = 0;

	uint8_t escape;
	
	while (1)
	{
		unsigned int uartChar = uart_getc();
		
		if (mode != PCCONT) //Wenn aus anderer Stelle der Mode verlassen werden soll...
		{
			fillPattern(0x00);
			//leavePcContionousMode = 0;
			return;
		}
		
		//Warten auf neues Zeichen
		if (!(uartChar & UART_NO_DATA))
		{
			// Go into sync escape mode
			if (uartChar == 0xff)
			{
				uart_puts("ESCAPE\r\n");
				do 
				{
					uartChar = uart_getc();
				}
				while (uartChar & UART_NO_DATA);
			 
				 // Get the next byte
				 //uartChar = uart_getc();

				 // Sync signal is received.
				 // Reset x and y counters to 0.
				 if (uartChar == 0x00)
				 {
					y = 0;
					z = 0;
					escape = 1;
					uart_puts("RESET-CNT\r\n");
				 }
				 else if (uartChar == 0x01)
				 {
					 uart_puts("Leave PC Continous Mode\r\n");
					 mode = AUTO;
					 return;
				 }
				 // if no 0x00 byte is received, proceed with
				 // the byte we just received.
			}

			if (escape == 0)
			{
				// Load data into the current position in the buffer
				serialBuffer[z][y] = (uint8_t)uartChar;

    			if (y == 7)
    			{
    				if (z == 7)
    				{
    					//Ein Frame ist komplett geladen - resette Counter
    					z = 0;
    					y = 0;
						
						//Kopieren der Daten vom seriellen Puffer in den echten Cube-Buffer
    					memcpy(cubeData, serialBuffer, CUBE_BYTES);
    				} else
    				{
    					//Layer komplett geladen. Setze y zurueck und inkrementiere z
    					z++;
    					y = 0;
    				}
    			} else
    			{
    				//Solange man einen Layer am befuellen ist wird y inkrementiert
    				y++;
    			}
	
			} else
			{
				escape = 0;
			}
		}
	}
}


void pcConfigMode()
{
	uint8_t escape = 0;
	
	while (1)
	{
		unsigned int uartChar = uart_getc();
		
		if (mode != PCCONFIG) //Wenn aus anderer Stelle der Mode verlassen werden soll...
		{
			fillPattern(0x00);
			//leavePcContionousMode = 0;
			return;
		}
		
		//Warten auf neues Zeichen
		if (!(uartChar & UART_NO_DATA))
		{
			// Go into escape mode
			//if (uartChar == 0xff)
			if (uartChar == 'e')
			{
				uart_puts("ESCAPE\r\n");
				do 
				{
					uartChar = uart_getc();
				}
				while (uartChar & UART_NO_DATA);
			 
			
				 //if (uartChar == 0xE0) //Conroller soll Userdefined Strings an den PC senden
				 if (uartChar == 't')
				 {
					 escape = 1;
					 
					uart_puts_P("Sende User Defined Strings:\r\n");
					
					char tmpString[23];
					
					for (uint8_t i=0; i<6; i++)
					{
						font_userDefinedString_getCompleteData(i,tmpString);
						uart_puts(tmpString);
						uart_puts("\r\n");
					}						
				 }
				 else if (uartChar == 0x01)
				 {
					 uart_puts("Leave PC Config Mode\r\n");
					 mode = AUTO;
					 return;
				 }
				 // if no 0x00 byte is received, proceed with
				 // the byte we just received.
			}

			if (escape == 0)
			{
				;
	
			} else
			{
				escape = 0;
			}
		}			
}
}

void lcd_clearAnimationName()
{
	lcd_gotoxy(0,3);
	lcd_puts_P("                ");
	lcd_gotoxy(0,3);
}

void lcd_clearMode()
{
	lcd_gotoxy(0,2);
	lcd_puts_P("                ");
	lcd_gotoxy(0,2);
}